package com.neoCalf.neoFile.service.impl;


import cn.hutool.core.util.StrUtil;
import com.google.protobuf.ServiceException;
import com.neoCalf.neoCommon.constants.Constants;
import com.neoCalf.neoCommon.enums.FileTypeEnum;
import com.neoCalf.neoCommon.enums.ResultCode;
import com.neoCalf.neoCommon.exception.BusinessException;
import com.neoCalf.neoCommon.model.dto.UploadDto;
import com.neoCalf.neoCommon.model.vo.UpLoadImageVo;
import com.neoCalf.neoFile.pro.MinioProp;
import com.neoCalf.neoFile.service.UpLoadService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/15 22:38
 */
@Service("upLoadService")
@Slf4j
public class UpLoadServiceImpl implements UpLoadService {
    @Resource
    private MinioClient minioClient;
    @Resource
    private MinioProp minioProp;

    @Override
    public UpLoadImageVo uploadImage(String userId, UploadDto uploadDto) {
        FileTypeEnum fileTypeByType = FileTypeEnum.getFileTypeByType(uploadDto.getFileType());
        if (fileTypeByType == null) {
            throw new BusinessException(ResultCode.Fail, "文件类型错误");
        }
        if (uploadDto.getFile() == null) {
            throw new BusinessException(ResultCode.Fail, "文件不能为空");
        }
        if (StrUtil.isEmpty(uploadDto.getFileId())) {
            throw new BusinessException(ResultCode.Fail, "文件id不能为空");
        }
        MultipartFile file = uploadDto.getFile();
        String fileId = uploadDto.getFileId();
        try {
            String fileName = fileTypeByType.getPath() + "/";
            switch (fileTypeByType) {
                case FILE:
                    fileName += fileId + "." + StrUtil.subAfter(file.getOriginalFilename(), ".", true);
                    break;
                case AVATAR:
                    fileName += fileId + Constants.PNG_SUFFIX;
                    deleteImage(minioProp.getEndpoint() + '/' + minioProp.getBucket() + '/' + fileName);
                    break;
            }

            //上传
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(minioProp.getBucket())
                    .object(fileName)
                    .contentType(file.getContentType())
                    .stream(file.getInputStream(), file.getInputStream().available(), -1)
                    .build());
            // 返回结果
            log.info("文件上传成功，{}", fileName);
            return new UpLoadImageVo(fileName, minioProp.getEndpoint() + '/' + minioProp.getBucket() + '/' + fileName);
        } catch (Exception e) {
            log.error(e.toString());
            throw new BusinessException(ResultCode.Fail, "上传失败");
        }
    }

    @Override
    public void deleteImage(String url) {
        try {
            String baseUrl = minioProp.getEndpoint() + "/" + minioProp.getBucket();
            String objectName = url.replace(baseUrl, "");
            RemoveObjectArgs args = RemoveObjectArgs.builder().bucket(minioProp.getBucket())
                    .object(objectName)
                    .build();
            minioClient.removeObject(args);
            log.info("文件删除成功，{}", objectName);
        } catch (Exception e) {
            log.error(e.toString());
        }
    }
}
